package mylog

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"io"
	"os"
)

var log *logrus.Logger

func LogInit(logLevel string) {
	log = logrus.New()

	level := logrus.DebugLevel
	log.SetLevel(logrus.ErrorLevel)
	switch {
	case logLevel == "debug":
		level = logrus.DebugLevel
	case logLevel == "info":
		level = logrus.InfoLevel
	case logLevel == "error":
		level = logrus.ErrorLevel
	default:
		level = logrus.DebugLevel
	}
	log.Formatter = &logrus.TextFormatter{
		ForceColors: false,
	}
	log.SetLevel(level)
	logFile, logErr := os.OpenFile("etcdview.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
	if logErr != nil {
		fmt.Println("Fail to find", *logFile, "cServer start Failed")
		os.Exit(1)
	}
	// 设置日志输出到文件
	// 定义多个写入器
	writers := []io.Writer{
		logFile,
		os.Stdout}

	fileAndStdoutWriter := io.MultiWriter(writers...)
	log.Out = fileAndStdoutWriter

}
func Println(v ...interface{}) {
	log.Info(v)
}

func Error(v ...interface{}) {
	log.Error(v)
}

func Info(v ...interface{}) {
	log.Info(v)
}
func WithField(key string, value interface{}) *logrus.Entry {
	return log.WithField(key, value)
}
